package com.das.mechanic_base.widget;

import android.content.Context;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.das.mechanic_base.R;
import com.das.mechanic_base.adapter.create.AddStaffAdapter;
import com.das.mechanic_base.adapter.create.AddStaffBean;
import com.das.mechanic_base.base.X3BaseBottomSheetDialog;
import com.das.mechanic_base.base.X3IBaseView;
import com.das.mechanic_base.mapi.api.NetWorkHttp;
import com.das.mechanic_base.mapi.response.HttpCallBack;
import com.das.mechanic_base.mapi.schedulers.RxSchedulersHelper;
import com.das.mechanic_base.utils.X3ToastUtils;
import com.kproduce.roundcorners.RoundTextView;

import java.util.List;

public class X3BottomAddStaffDialog extends X3BaseBottomSheetDialog implements View.OnClickListener, AddStaffAdapter.IOnClickSelectStaff {

    private X3MaxHeightRecyclerView rlv_staff;
    private RoundTextView rv_cancel;
    private RoundTextView rv_affirm;
    private AddStaffAdapter staffAdapter;
    private List<Long> sList;
    private long carId;

    public X3BottomAddStaffDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected boolean getDialogCancel() {
        return false;
    }

    @Override
    protected void initView() {
        rlv_staff = getView(R.id.rlv_staff);
        rv_cancel = getView(R.id.rv_cancel);
        rv_affirm = getView(R.id.rv_affirm);

        rlv_staff.setLayoutManager(new LinearLayoutManager(mContext));
        staffAdapter = new AddStaffAdapter(mContext);
        rlv_staff.setAdapter(staffAdapter);
        staffAdapter.setiOnClickSelectStaff(this);

        rv_affirm.setOnClickListener(this);
        rv_cancel.setOnClickListener(this);

    }

    public void showAllStaff(long carId) {
        this.carId = carId;
        NetWorkHttp.getApi().listFollowupMechanicForCarOwnerUser(carId)
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .compose(((X3IBaseView)mContext).bindToLife())
                .subscribe(new HttpCallBack<List<AddStaffBean>>() {
                    @Override
                    protected void onDone(List<AddStaffBean> addStaffBeans) {
                        if (staffAdapter != null) {
                            staffAdapter.changeStaff(addStaffBeans);
                        }
                    }

                    @Override
                    protected void showError(String error) {

                    }
                });

    }

    @Override
    protected int getLayoutId() {
        return R.layout.x3_bottom_add_staff_dialog;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.rv_cancel) {
            dismiss();

        }else if (v.getId() == R.id.rv_affirm) {
            submitStaffList();
        }
    }

    private void submitStaffList() {
        NetWorkHttp.getApi().reverseAddFollowup(carId,sList)
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .compose(((X3IBaseView)mContext).bindToLife())
                .subscribe(new HttpCallBack<Object>() {
                    @Override
                    protected void onDone(Object o) {
                        if (iOnRefresh != null) {
                            iOnRefresh.iOnRefreshStaff();
                        }
                        dismiss();
                    }

                    @Override
                    protected void showError(String error) {
                        X3ToastUtils.showMessage(error);
                    }
                });

    }

    IOnRefresh iOnRefresh;

    public X3BottomAddStaffDialog setiOnRefresh(IOnRefresh iOnRefresh) {
        this.iOnRefresh = iOnRefresh;
        return this;
    }

    public interface IOnRefresh {
        void iOnRefreshStaff();
    }

    @Override
    public void iOnSelectStaff(List<Long> sList) {
        this.sList = sList;
    }
}
